use axum::Json;
use serde_json::Value;
use tokio::fs;

use crate::code;

use super::utils::{ exist, exist_rel, get_rel_path };

pub async fn remove_file(path:&str) -> Result<Json<Value>, Json<Value>>{
    if exist(&path)? {
        let del = fs::remove_file(&path).await;
        match del {
            Ok(_f) => code::ok(),
            Err(e) => code::sys(e.to_string())
        }
    }else{
        code::ok()
    }
}

pub async fn remove_file_rel(path:&str) -> Result<(), Json<Value>>{
    let path = get_rel_path(path);
    if exist_rel(&path)? {
        let del = fs::remove_file(&path).await;
        match del {
            Ok(_f) => Ok(()),
            Err(e) => Err( code::sys_(e.to_string()) )
        }
    }else{
        Ok(())
    }
}

pub async fn remove_dir_all_rel(path:&str) -> Result<(), Json<Value>>{
    let path = get_rel_path(path);
    if exist_rel(&path)? {
        let del = fs::remove_dir_all(&path).await;
        match del {
            Ok(_f) => Ok(()),
            Err(e) => Err( code::sys_(e.to_string()) )
        }
    }else{
        Ok(())
    }
}

pub async fn remove_files_rel(paths:Vec<String>) -> Result<(), Json<Value>>{
    if paths.is_empty() { return Ok(()) }
    for v in paths{
        remove_file_rel(&v).await?
    }
    Ok(())
}